package com.enqualcomm.kids.mvp.terminalinfo;

import com.android.volley.VolleyError;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.network.NetworkListener;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.QueryUserTerminalInfoParams;
import com.enqualcomm.kids.network.socket.request.UpdateUserTerminalInfoParams;
import com.enqualcomm.kids.network.socket.response.BasicResult;
import com.enqualcomm.kids.network.socket.response.QueryUserTerminalInfoResult;

/**
 * Created by Sunzhigang on 2015/12/21.
 *
 */
public class TerminalInfoModel extends NetworkModel implements ITerminalInfoModel {
    @Override
    public void getTerminalInfo(String userkey, String userterminalid, final TerminalInfoHandler handler) {
        QueryUserTerminalInfoParams params = new QueryUserTerminalInfoParams(userkey,userterminalid);
        loadDataFromServer(new SocketRequest<QueryUserTerminalInfoResult>(params, new NetworkListener<QueryUserTerminalInfoResult>() {
            @Override
            public void onError(VolleyError error) {
                handler.getTerminalInfoFailed("");
            }

            @Override
            public void onSuccess(QueryUserTerminalInfoResult response) {
                if (response.code == 0){
                    handler.getTerminalInfoSuccess(response.result.get(0));
                }else {
                    handler.getTerminalInfoFailed("获取失败:"+response.code+"-"+response.desc);
                }

            }
        }));
    }

    @Override
    public void setTerminalInfo(String userKey, String terminalid, QueryUserTerminalInfoResult.Data info, final TerminalInfoHandler handler) {
        UpdateUserTerminalInfoParams params = new UpdateUserTerminalInfoParams(userKey,
                terminalid,
                info.birthday,
                info.grade,
                info.mobile,
                info.name,
                info.relation,
                info.height,
                info.weight,
                info.gender,
                info.mobile2,
                info.mobiletype,
                info.areacode,
                info.areacode2);
        loadDataFromServer(new SocketRequest<BasicResult>(params, new NetworkListener<BasicResult>() {
            @Override
            public void onError(VolleyError error) {
                handler.setTerminalInfoFailed("");
            }

            @Override
            public void onSuccess(BasicResult response) {
                if (response.code ==0){
                    handler.setTerminalInfoSuccess();
                }else {
                    handler.setTerminalInfoFailed("设置失败:"+response.code+"-"+response.desc);
                }

            }
        }));
    }
}
